package com.ftg.learn.remote;

import com.ftg.learn.common.dto.LoginDto;
import com.ftg.learn.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author 布衣
 */
@Component
public class AuthRemote {


    /*******************************restTemplate - okhttp3**********************************/
    @Autowired
    RestTemplate restTemplate;


    private List<String> ips = new ArrayList<>();
    {
//        ips.add("http://localhost:8081");
        ips.add("http://localhost:8083");
    }

    public R<LinkedHashMap> callSystemUserLogin(LoginDto loginDto) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON); // 设置 Content-Type 为 JSON

        HttpEntity<LoginDto> requestEntity = new HttpEntity<>(loginDto, headers);
        //高可用下的，服务并存问题

        //负载均衡：
        //随机：
        int r = (int)(Math.random() * ips.size());
        System.out.println("随机数：" + ips.get(r));
        return restTemplate.postForObject(ips.get(r) +"/login/login",requestEntity,R.class);
    }

}
